using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace AiQiuQuan.Sport.WebApi.Controllers
{
    /// <summary>
    /// 平台比赛管理
    /// </summary>
    public class PlatformGameController : PcApiController
    {
        private readonly IGameService _gameService;

        /// <summary>
        /// 比赛管理
        /// </summary>
        public PlatformGameController(
            IGameService gameService
            )
        {
            _gameService = gameService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id">业务编号</param>
        [HttpGet("get")]
        public async Task<UnaryResult<GameDto>> GetAsync([Required] Guid id)
        {
            var result = await _gameService.GetAsync(id);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 比赛分页请求
        /// </summary>
        [HttpPost("pagelist")]
        public async Task<UnaryResult<PageResult<GameDto>>> PageListAsync(GameQueryPageRequestDto request)
        {
            request.IsPlatform = true;
            var result = await _gameService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost("add")]
        public async Task<UnaryResult> AddAsync(GameAddDto model)
        {
            var result = await _gameService.AddAsync(model);
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost("update")]
        public async Task<UnaryResult> UpdateAsync(GameUpdateDto model)
        {
            var result = await _gameService.UpdateAsync(model);
            return result;
        }

        /// <summary>
        /// 比赛状态修改
        /// </summary>
        [HttpPost("update_state")]
        public async Task<UnaryResult> UpdateGameStateAsync(GameStateUpdateDto request)
        {
            var result = await _gameService.UpdateGameStateAsync(request);
            return result;
        }

        /// <summary>
        /// 增加比赛管理员
        /// </summary>
        [HttpPost("add_manage")]
        public async Task<UnaryResult> AddManageUserAsync(GameManageUserAddDto request)
        {
            var result = await _gameService.AddManageUserAsync(request);
            return result;
        }

        /// <summary>
        /// 删除比赛管理员
        /// </summary>
        [HttpPost("delete_manage")]
        public async Task<UnaryResult> DeleteManageUserAsync(GameManageUserDeleteDto request)
        {
            var result = await _gameService.DeleteManageUserAsync(request);
            return result;
        }

        /// <summary>
        /// 获取比赛管理员
        /// </summary>
        /// <param name="gameId">比赛ID</param>
        [HttpGet("list_manage")]
        public async Task<UnaryResult<List<GameManageUserDto>>> GetGameManageUserListAsync([Required] Guid gameId)
        {
            var result = await _gameService.GetGameManageUserListAsync(gameId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpGet("delete")]
        public async Task<UnaryResult> DeleteAsync(Guid id)
        {
            var result = await _gameService.DeleteAsync(id);
            return result;
        }

        /// <summary>
        /// 非团体比赛队伍列表
        /// </summary>
        [HttpPost("list_team_single")]
        public async Task<UnaryResult<List<GameTeamDto>>> GetGameTeamListAsync(GameTeamSingleRequestDto request)
        {
            var result = await _gameService.GetSingleGameTeamListPCAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 团体比赛队伍概要列表
        /// </summary>
        [HttpPost("list_team_simple")]
        public async Task<UnaryResult<List<GameTeamSimpleDto>>> GetSimpleGameTeamListAsync(GameTeamRequestDto request)
        {
            var result = await _gameService.GetSimpleGameTeamListPCAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 团体比赛队伍列表
        /// </summary>
        /// <param name="businessId">队伍业务ID</param>
        [HttpGet("list_team")]
        public async Task<UnaryResult<List<GameTeamDto>>> GetGameTeamListAsync([Required] Guid businessId)
        {
            var result = await _gameService.GetGameTeamListPCAsync(businessId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 小组信息更改
        /// </summary>
        [HttpPost("update_group")]
        public async Task<UnaryResult> UpdateGroupAsync(GameGroupUpdateDto request)
        {
            var result = await _gameService.UpdateGroupAsync(request);
            return result;
        }

        /// <summary>
        /// 获取比赛小组列表
        /// </summary>
        [HttpPost("list_group")]
        public async Task<UnaryResult<List<GameGroupDto>>> GetGameGroupListAsync(GameGroupRequestDto request)
        {
            var result = await _gameService.GetGameGroupListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取小组信息及对阵信息列表
        /// </summary>
        [HttpPost("list_group_user_versus")]
        public async Task<UnaryResult<List<GameGroupDto>>> GetSingleGameGroupAndVersusInfo(GameGroupRequestDto request)
        {
            var result = await _gameService.GetGameGroupAndVersusListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取小组抽签信息
        /// </summary>
        [HttpPost("get_group_draw_info")]
        public async Task<UnaryResult<GameGroupDrawInfoDto>> GetGroupDrawInfoAsync(GameRequestDto request)
        {
            var result = await _gameService.GetGroupDrawInfoAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 小组自动抽签
        /// </summary>
        [HttpPost("group_auto_draw")]
        public async Task<UnaryResult<List<GameGroupDrawTeamDto>>> GroupAutoDrawAsync(GameRequestDto request)
        {
            var result = await _gameService.GroupAutoDrawAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 结束小组赛
        /// </summary>
        /// <param name="groupId">小组ID</param>
        [HttpGet("finish_group")]
        public async Task<UnaryResult> FinishGameGroupAsync([Required] Guid groupId)
        {
            var result = await _gameService.FinishGameGroupAsync(groupId);
            return result;
        }

        /// <summary>
        /// 保存小组抽签
        /// </summary>
        [HttpPost("group_draw")]
        public async Task<UnaryResult> SaveGroupDrawLotsAsync(GameGroupDrawLotsRequestDto request)
        {
            var result = await _gameService.SaveGroupDrawLotsAsync(request);
            return result;
        }

        /// <summary>
        /// 小组比分输入
        /// </summary>
        [HttpPost("finish_group_versus")]
        public async Task<UnaryResult> FinishGroupVersusAsync(GameGroupVersusScoreAddDto request)
        {
            var result = await _gameService.FinishGroupVersusAsync(request);
            return result;
        }

        /// <summary>
        /// 小组排名计算
        /// </summary>
        [HttpPost("calculate_group_rank")]
        public async Task<UnaryResult> CalculateGroupRankAsync(GameGroupRankRequestDto request)
        {
            var result = await _gameService.CalculateGroupRankAsync(request);
            return result;
        }

        /// <summary>
        /// 淘汰赛比分输入
        /// </summary>
        [HttpPost("finish_konckout_versus")]
        public async Task<UnaryResult> FinishKnockoutVersusAsync(GameKnockoutVersusScoreAddDto request)
        {
            var result = await _gameService.FinishKnockoutVersusAsync(request);
            return result;
        }

        /// <summary>
        /// 获取比赛轮次列表
        /// </summary>
        [HttpPost("list_rounds")]
        public async Task<UnaryResult<List<GameKnockoutRoundsDto>>> GetGameRoundsListAsync(GameRequestDto request)
        {
            var result = await _gameService.GetGameRoundsListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 淘汰赛对阵列表
        /// </summary>
        [HttpPost("list_knockout_versus")]
        public async Task<UnaryResult<List<GameKnockoutVersusDto>>> GetGameKnockoutVersusListAsync(GameKnockoutVersusRequestDto request)
        {
            var result = await _gameService.GetGameKnockoutVersusListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 淘汰赛对阵信息修改
        /// </summary>
        [HttpPost("update_knockout_versus")]
        public async Task<UnaryResult> UpdateGameKnockoutVersusInfoAsync([Required] List<GameKnockoutVersusUpdateDto> request)
        {
            var result = await _gameService.UpdateGameKnockoutVersusInfoAsync(request);
            return result;
        }

        /// <summary>
        /// 结束淘汰赛轮次比赛
        /// </summary>
        /// <param name="knockoutRoundsId">轮次ID</param>
        [HttpGet("finish_konckout_rounds")]
        public async Task<UnaryResult> FinishKnockoutRoundsAsync([Required] Guid knockoutRoundsId)
        {
            var result = await _gameService.FinishKnockoutRoundsAsync(knockoutRoundsId);
            return result;
        }

        /// <summary>
        /// 自动报名比赛
        /// </summary>
        [HttpGet("auto_join")]
        public async Task<UnaryResult> AutoJoinAsync(Guid gameId, bool isDraw = false)
        {
            var result = await _gameService.AutoJoinAsync(gameId, isDraw);
            return result;
        }
    }
}
